package com.yc.sole.system.controller;

import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.system.request.CacheQueryReq;
import com.yc.sole.system.request.CacheReq;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.CacheBiz;
import com.yc.sole.system.response.CacheResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 缓存管理
 *
 * @author yizuomin
 * @date 2023/5/26 09:42
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class CacheController {

    private final CacheBiz cacheBiz;

    /**
     * 根据缓存id获取缓存详情
     *
     * @param cacheId
     * @return
     */
    @PostMapping("/api/system/cache/getKeys/{cacheId}")
    @PreAuth(value = "system:cache:select", description = "根据缓存id获取缓存详情")
    public Result<List<CacheResp>> getKeys(@PathVariable Long cacheId) {
        return Result.data(cacheBiz.getKeys(cacheId));
    }

    /**
     * 根据缓存编码获取缓存详情
     *
     * @param key
     * @return
     */
    @PostMapping("/api/system/cache/getDetail/{cacheId}/{key}")
    @PreAuth(value = "system:cache:select", description = "根据缓存编码获取缓存详情")
    public Result<Object> getDetailByKey(@PathVariable Long cacheId, @PathVariable String key) {
        return Result.data(cacheBiz.getDetailByKey(cacheId, key));
    }

    /**
     * 根据key删除缓存
     *
     * @param key
     * @return
     */
    @PostMapping("/api/system/cache/delete/{cacheId}/{key}")
    @PreAuth(value = "system:cache:delete", description = "根据key删除缓存")
    public Result<Object> deleteKey(@PathVariable Long cacheId, @PathVariable String key) {
        cacheBiz.deleteKey(cacheId, key);
        return Result.success();
    }

    /**
     * 新增
     *
     * @param cacheReq
     * @return RestResult
     */
    @PreAuth(value = "system:cache:save", description = "新增缓存配置")
    @PostMapping("/api/system/cache/saveCache")
    public Result<Void> saveCache(@RequestBody @Validated(SaveGroup.class) CacheReq cacheReq) {
        cacheBiz.saveCache(cacheReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param cacheReq
     * @return RestResult
     */
    @PreAuth(value = "system:cache:update", description = "修改缓存配置")
    @PostMapping("/api/system/cache/updateCacheById")
    public Result<Void> updateCacheById(@RequestBody @Validated(UpdateGroup.class) CacheReq cacheReq) {
        cacheBiz.updateCacheById(cacheReq);
        return Result.success();
    }

    /**
     * 通过Id查询缓存配置
     *
     * @param cacheId
     * @return RestResult
     */
    @PreAuth(value = "system:cache:select", description = "通过Id查询缓存配置")
    @PostMapping("/api/system/cache/selectCacheById/{cacheId}")
    public Result<CacheResp> selectCacheById(@PathVariable Long cacheId) {
        return Result.data(cacheBiz.selectCacheById(cacheId));
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "system:cache:delete", description = "删除缓存配置")
    @PostMapping("/api/system/cache/deleteCache")
    public Result<Void> deleteCache(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        cacheBiz.deleteCache(baseDeleteReq.getIds());
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "system:cache:select", description = "分页查询缓存配置")
    @PostMapping("/api/system/cache/selectCachePage")
    public Result<Paging<CacheResp>> selectCachePage(@RequestBody @Validated BasePageReq<CacheQueryReq> basePageReq) {
        return Result.data(cacheBiz.selectCachePage(basePageReq.toPage(), basePageReq.getQuery()));
    }
}
